var appletWidth = 600;
var appletHeight = 500;
var maxDistance = 250;

// Code to load applet
var graph = {
	"_object" : undefined,
	"attributes" : {
		"code" : 'friendNet.class',
		"archive" : './applet/friendNet.jar,./applet/physics.jar,./applet/core.jar,./applet/json.jar',
		"width" : 600,
		"height" : 500,
		"image" : './applet/loading.gif',
		"id" : 'friendsNet'
	}, 
	"parameters" :{
		"classloader_cache" : false,
		"separate_jvm" : true
	},
	"version" : "1.5",
	"width" : 600,
	"height" : 500,
	"maxDistance" : 250,
	"maxScore" : 0,
	"init": function(){
		try{
			if(this._object == undefined){
				this._object = document.getElementById('friendsNet');
			}
		}catch(err){trace2(err);}
	},
	"setDefault": function(){
		try{
			
			// set weights
			this.setWeight();

			// set default threshold
			this.setThreshold(default_threshold);
			
			// show graph
			this.show();
			
		}catch(err){trace2(err);}
	},
	"getInstance": function(){
		try{
			if(this._object == undefined){
				this.init();
			}
			return this._object;
		}catch(err){trace2(err);}
	},
	"draw" : function() {
		try {
			// check global variable isLoaded if data is loaded
			if (isLoaded.applet && isLoaded.friend) {

				// get object
				if(this._object == undefined){
					this.init();
				}

				this.setDefault();

				this.refresh();

			}
		}catch(err){trace2(err);}
	},
	"clearGraph" : function() {
		try {
			if(this._object == undefined){
				this.init();
			}
			this._object.clearGraph();
			
		}catch(err){trace2(err);}
	},
	"checkUp": function(){
		try{
			if(this._object == undefined){
				this.init();
			}
			this._object.isUp();
			
			/* applet loaded */
			return true;
			
		}catch(err){
			return false;
		}
	},
	"show" : function() {
		try {

			if(this._object == undefined){
				this.init();
			}
		}catch(err){trace2(err);}
		try{
			this._object.setAttribute("style", "visibility:visible;");
		}catch(err){trace2(err);}

		try{
			$('.switchboard').show();
			$('.buttons_container').show();
			$('#settings-container').dialog("open");
		}catch(err){trace2(err);}


	},
	"hide" : function() {
		try {

			if(this._object == undefined){
				this.init();
			}
			this._object.setAttribute("style", "visibility:hidden;");
			
		}catch(err){trace2(err);}
	},
	"setWeight" : function() {
		try {
			if(this._object == undefined){
				this.init();
			}
			// set weights
			this._object.setMessageWeight($('#message_weight_slider').slider("value"));
			this._object.setPostWeight($('#post_weight_slider').slider("value"));
			this._object.setCommentWeight($('#comment_weight_slider').slider("value"));
			this._object.setLikeWeight($('#like_weight_slider').slider("value"));
		}catch(err){trace2(err);}
	},
	"setChecked" : function(type, isChecked) {
		try {
			if(this._object == undefined){
				this.init();
			}

			if (type == "message") {
				this._object.setMessageChecked(isChecked);
			}
			if (type == "post") {
				this._object.setPostChecked(isChecked);
			}
			if (type == "comment") {
				this._object.setCommentChecked(isChecked);
			}
			if (type == "like") {
				this._object.setLikeChecked(isChecked);
			}
		}catch(err){trace2(err);}
	},
	"setThreshold" : function(threshold) {
		try {

			if(this._object == undefined){
				this.init();
			}
			this._object.setThreshold(threshold);
		}catch(err){trace2(err);}
	},
	"setMessageWeight" : function(value) {
		try {
			if(this._object == undefined){
				this.init();
			}
			this._object.setMessageWeight(value);
		}catch(err){trace2(err);}
	},
	"setPostWeight" : function(value) {
		try {
			if(this._object == undefined){
				this.init();
			}
			this._object.setPostWeight(value);
		}catch(err){trace2(err);}
	},
	"setCommentWeight" : function(value) {
		try {
			if(this._object == undefined){
				this.init();
			}
			this._object.setCommentWeight(value);
		}catch(err){trace2(err);}
	},
	"setLikeWeight" : function(value) {
		try {
			if(this._object == undefined){
				this.init();
			}
			this._object.setLikeWeight(value);
		}catch(err){trace2(err);}
	},
	"addCount" : function(id, type, count) {
		try {
			if(this._object == undefined){
				this.init();
			}
			this._object.addCount(id, type, count);
		}catch(err){
			trace2('fail, id:'+id+', type:'+type+', count:'+count);
			trace2(err);
		}
	},
	"addToGraph": function(data){
		try{
			if(this._object == undefined){
				this.init();
			}
			
			//if type of the input data is not string, stringify it 
			if(typeof data !== 'string'){
				data = JSON.stringify(data);
			}
			
			this._object.addJSONData(data);
				
			
		}catch(err){
			trace2(err);
		}		
	},
	"refresh" : function(){
		try{
			this.clearGraph();
			
			// draw friends
			this.addToGraph(friends);
			
			//set count
			this.addToGraph(data);
			
		}catch(err){trace2(err);}
	},
	"getNumberOfFriends" : function(){
		try{
			if(this._object == undefined){
				this.init();
			}
			
			return this._object.getNumberOfFriends();
			
		}catch(err){trace2(err);}
	},
	"error": false,
};